//
// Created by fazhehy on 2024/3/24.
//

#include "motor.h"

void motor_init()
{
    pwm_init(TCPWM_CH04_P04_0, 17000, 0);
    gpio_init(P04_1, GPO, GPIO_HIGH, GPO_PUSH_PULL);
}

void motor_set_duty(int16_t duty)
{
    if (duty > 0){
        gpio_set_level(P04_1, GPIO_LOW);
    }
    else{
        duty = -duty;
        gpio_set_level(P04_1, GPIO_HIGH);
    }
    duty = duty > MOTOR_MAX_DUTY ? MOTOR_MAX_DUTY : duty;
    pwm_set_duty(TCPWM_CH04_P04_0, duty);
}

void motor_stop()
{
    pwm_set_duty(TCPWM_CH04_P04_0, 0);
}
